<template>
	<view style="width: 100%;">
		<view class="top-search">
			<uni-nav-bar backgroundColor="white" :fixed="true" color="black" borderColor="#efefef" status-bar title="工作交办" leftIcon="arrow-left" @clickLeft="on_back"/>
			<view style="height: 3px;background-color: #efefef;"></view>
		</view>
		<view :style="'height:'+top_height+'px'"></view>
		<view style="height: 3px;"></view>
		<view style="width: 100%;display: flex;flex-direction: row;height: auto;">
			<view style="width: 1%;"></view>
			<view style="width: 98%;">
				<view class="lr_container">
					<!-- 下拉 -->
					<view class="lr_view">
						<view class="lr_view_txt">类型：</view>
						<view class="lr_view_pick">
							 <picker mode="selector" :value="lxIndex" :range="lxlist" range-key="name" @change="onLxChange">
								{{lxTxt}}
							</picker>
						</view>
						<view class="lr_view_end"><text style="color: red;">*</text></view>
					</view>
					<view class="lr_line"></view>
					<!-- 下拉 -->
					<view class="lr_view">
						<view class="lr_view_txt">接收人：</view>
						<view class="lr_view_pick">
							<picker mode="selector" :value="zgIndex" :range="zglist" range-key="xm" @change="onZgChange">
								{{zgTxt}}
							</picker>
						</view>
						<view class="lr_view_end"><text style="color: red;">*</text></view>
					</view>
					<view class="lr_line"></view>
					<!-- 下拉 -->
					<view class="lr_view">
						<view class="lr_view_txt">项目：</view>
						<view class="lr_view_pick">
							 <picker mode="selector" :value="xmIndex" :range="xmlist" range-key="xmmc" @change="onXmChange">
								{{xmTxt}}
							</picker>
						</view>
						<view class="lr_view_end"></view>
					</view>
					<view class="lr_line"></view>
					<!-- 输入框 -->
					<view class="lr_view">
						<view class="lr_view_txt">交办事宜：</view>
						<view class="lr_view_input">
							<textarea auto-height v-model="jbsy"></textarea>
						</view>
						<view class="lr_view_end"><text style="color: red;">*</text></view>
					</view>
					<view class="lr_line"></view>
					<!-- 输入框 -->
					<view class="lr_view">
						<view class="lr_view_txt">图片附件：</view>
						<view class="lr_view_input">
							<button @click="chooseImage">选择图片</button>
						</view>
						<view class="lr_view_end"></view>
					</view>
					<view class="lr_line"></view>
					<!-- 输入框 -->
					<view class="lr_view">
						<view class="lr_view_txt">截止日期：</view>
						<view class="lr_view_input">
							<picker mode="date" :start="startDate" @change="bindDateChange">
								<view class="picker">
									{{selectedDate}}
								</view>
							</picker>
						</view>
						<view class="lr_view_end"><text style="color: red;">*</text></view>
					</view>
					<view class="lr_line" style="height: 12px;"></view>
					<view class="lr_btn">
						<button :disabled="isDis" class="lr_btn" @tap="qdjb">确定交办</button>
					</view>
				</view>
			</view>
			<view style="width: 1%;"></view>
		</view>
	</view>

</template>

<script>
	var _self;
	import utils from '../../common/utils'
	export default {
	  data() {
	    return {
			top_height:'20',
			//职工
			listInfo:'',
			lxlist:'',
			xmlist:'',
			zglist:'',
			lxIndex: 0,
			xmIndex: 0,
			zgIndex: 0,
			lxTxt:'',
			xmTxt:'可以不选择项目',
			zgTxt:'',
			jbsy:'',
			startDate: this.getToday(),
			selectedDate: this.getToday(),
			imagePaths: [],
			isDis:false
	    };
	  },
	  methods: {
		chooseImage() {
			uni.chooseImage({
				sourceType: ['album', 'camera'],
				count: 1, 
				success: (res) => {
					this.imagePaths = res.tempFilePaths;
				},
				fail: (err) => {
					uni.showToast({
						title: `选择图片失败: ${err.errMsg}`,
						icon: 'none'
					});
				}
			});
		  },
		  onLxChange(e) {
			this.lxIndex = e.detail.value;
			this.lxTxt = this.lxlist[this.lxIndex].name;
		  },
		  onXmChange(e) {
			this.xmIndex = e.detail.value;
			this.xmTxt = this.xmlist[this.xmIndex].xmmc;
		  },
		  onZgChange(e) {
			this.zgIndex = e.detail.value;
			this.zgTxt = this.zglist[this.zgIndex].xm;
		  },
		  getToday() {
			  const date = new Date();
			  const year = date.getFullYear();
			  const month = String(date.getMonth() + 1).padStart(2, '0');
			  const day = String(date.getDate()).padStart(2, '0');
			  return `${year}-${month}-${day}`;
		  },
		  bindDateChange(e) {
			  this.selectedDate = e.detail.value;
		  },
		  on_back(){
			uni.switchTab({
			url: '/pages/dbsy/dbsy'
			});
		  },
		  //初始化数据，加载职工信息等等
		  initPage: async function () {
		  	this.$utils.showLoading("数据加载中...");
		  	let data = {
		  		timestamp:this.$utils.getTimestamp()
		  	};
		  	let enData = this.$utils.encrypt(JSON.stringify(data),uni.getStorageSync("userInfo").aesKey,uni.getStorageSync("userInfo").aesIv);
		  	let postData = {
		  		data:""+enData+""
		  	}
		  	try {
		  		let res = await this.$myRequest.post_token(getApp().globalData.dbsyInfoForm,postData);
				console.info("initPage获取到的:"+JSON.stringify(res));
		  		this.$utils.hideLoading();
				// 停止下拉刷新动画
				uni.stopPullDownRefresh();
		  		if (res.code==0)
		  		{
					let a = JSON.parse(utils.decrypt(res.data,uni.getStorageSync("userInfo").aesKey,uni.getStorageSync("userInfo").aesIv));
					this.lxlist = a.xmDbsyLxList;
					this.xmlist = a.xmXmxxList;
					this.zglist = a.hsZgxxList;
					this.lxTxt = this.lxlist[this.lxIndex].name;
					this.zgTxt = this.zglist[this.zgIndex].xm;
					this.jbsy='';
					console.info("initPage获取到的a:"+JSON.stringify(a));
					const newObject = {
						id: 0,
						xmmc: '可以不选择项目'
					};
					this.xmlist.push(newObject);
					console.info("xmlist:"+JSON.stringify(this.xmlist));
		  		}
				else if (res.code==6){
					uni.setStorageSync('isLogin',false);
					//弹出登录页面
					uni.navigateTo({
						url: '/pages/login/login',
						animationType: 'pop-in',
						animationDuration: 200
					});
				}
		  		else{
		  			this.$utils.toast("数据加载失败！");
		  		}
		  	} catch (e) {  
		  		this.$utils.hideLoading();
		  		//console.info(e);
		  		this.$utils.toast("数据加载失败！");           
		  	}
		  },
		  qdjb: async function () {
			  this.isDis=true;
		  	this.$utils.showLoading("数据提交中...");
			// 构建 files 数组（关键！）
			const files = this.imagePaths.map((filePath, index) => {
				return {
				  name: 'wj', // 注意字段名需与后端约定
				  uri: filePath    // 文件路径（APP环境直接使用临时路径）
				};
			});
			console.info("files获取到的:"+files);
		  	let data = {
				lx:'0',
				lx_id:this.lxlist[this.lxIndex].id,
				jsr_id:this.zglist[this.zgIndex].id,
				dbsy:this.jbsy,
				jzrq:this.selectedDate,
				xmxx_id:this.xmlist[this.xmIndex].id,
				isApp:'1',
		  		timestamp:this.$utils.getTimestamp()
		  	};
		  	let enData = this.$utils.encrypt(JSON.stringify(data),uni.getStorageSync("userInfo").aesKey,uni.getStorageSync("userInfo").aesIv);
		  	let postData = {
		  		data:""+enData+""
		  	}
		  	try {
				var res;
				if (files.length>0){
					res = await this.$myRequest.post_file_token(getApp().globalData.dbsyAdd,files,postData);
					console.info("qdjb有上传获取到的:"+JSON.stringify(res));
				}else{
					res = await this.$myRequest.get_token(getApp().globalData.dbsyAdd,enData);
					console.info("qdjb无上传获取到的:"+JSON.stringify(res));
				}
		  		this.$utils.hideLoading();
				this.isDis=false;
		  		if (res.code==0)
		  		{
					uni.showToast({
						title: '数据提交成功！',
						icon: 'none',
						duration: 2000
					});
					setTimeout(()=>{
						_self.initPage();
					},2000);
		  		}
				else if (res.code==6){
					uni.setStorageSync('isLogin',false);
					//弹出登录页面
					uni.navigateTo({
						url: '/pages/login/login',
						animationType: 'pop-in',
						animationDuration: 200
					});
				}
		  		else{
		  			this.$utils.toast("数据提交失败！"+res.msg);
		  		}
		  	} catch (e) {  
		  		this.$utils.hideLoading();
		  		//console.info(e);
		  		this.$utils.toast("数据提交失败！"+e);           
		  	}
		  }
	  },onLoad() {
			_self = this;
			this.isLogin = utils.checkLogin();
			console.info("获取到的登录状态："+this.isLogin);
			if (this.isLogin){
				this.phone = uni.getStorageSync("userInfo").phone;
				this.initPage();
			}else{
				//弹出登录页面
				uni.navigateTo({
					url: '/pages/login/login?sftc=1',
					animationType: 'pop-in',
					animationDuration: 200
				});
			}
		},onShow() {
			_self = this;
			this.isLogin = utils.checkLogin();
			console.info("获取到的登录状态："+this.isLogin);
			if (this.isLogin){
			}else{
				//弹出登录页面
				uni.navigateTo({
					url: '/pages/login/login?sftc=1',
					animationType: 'pop-in',
					animationDuration: 200
				});
			}
		},onReady() {
			let that = this;
			/* 设置当前滚动容器的高，若非窗口的高度，请自行修改 */
			uni.getSystemInfo({
				success:(res)=>{
					let info = uni.createSelectorQuery().select(".top-search");
					info.boundingClientRect(function(data) { 
						//console.info("高度："+data.height);
						that.top_height = data.height;
					}).exec()
				}
			});
		}
	};
</script>

<style>
	page{
		background-color: #efefef;
		width: 100%;
	}
	.top-search{
		width: 100%;
		background-color: white;
		position: fixed;
		display: inline-block;
		color: #292933;
		z-index: 9999;
	}
</style>	